<template>
  <Panel>
    <template>
      <el-form
        :model="addForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="商品分类" prop="cate">
          <el-select v-model="addForm.cate" placeholder="请选择商品分类">
            <el-option
              v-for="(item, index) in cates"
              :key="index.cateName"
              :label="item.cateName"
              :value="item.cateName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input-number
            v-model="addForm.price"
            :min="1"
            :max="10"
          ></el-input-number
        ></el-form-item>
        <el-form-item label="商品图片">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:5000/goods/goods_img_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品描述" prop="desc">
          <el-input type="textarea" v-model="addForm.desc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >添加商品</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </template>
  </Panel>
</template>

<script>
// 10修改用户头像信息
import { allCates, addCom } from '../../api/good.js'
export default {
  data() {
    return {
      imageUrl: '',
      num: 1,
      addForm: {
        name: '',
        region: '',
        cate: '',
        price: 0,
        imgstr: '',
        desc: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        cate: [{ required: true, message: '请选择分类', trigger: 'change' }],
        price: [{ required: true, message: '请输入价格', trigger: 'change' }],
        /*  resource: [
          { required: true, message: '请选择活动资源', trigger: 'change' }
        ], */
        desc: [{ required: true, message: '请填写商品描述', trigger: 'blur' }]
      },
      // 给图片添加未提交图片会提示的事件
      goodimg: '',
      cates: []
    }
  },
  created() {
    this.getCates()
  },
  methods: {
    async getCates() {
      const { categories } = await allCates()
      // console.log(data) { categories } cateName
      //  this.cates = categories
      this.cates = categories
      // console.log(this.cates)
      /*  console.log(this.cates.dep.id) */
    },
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // d当图片为空就提示上传图片
          if (this.goodimg === '') {
            this.$message({
              type: 'warning',
              message: '请先上传图片'
            })
          }
          // 表单验证通过,图片也发起请求了, 才发请求
          console.log(this.addForm)
          const { code, msg } = await addCom({
            name: this.addForm.name,
            category: this.addForm.cate,
            price: this.addForm.price,
            imgUrl: this.goodimg,
            goodsDesc: this.addForm.desc
          })
          // console.log(code, msg)
          /*  alert('submit!') */
          // 如果验证通过了,就把信息储存然后通过计时器放入商品列表页面
          if (code === 0) {
            this.$message({
              type: 'success',
              message: msg
            })
          }
          setTimeout(() => {
            this.$router.push('/shangping/list')
          }, 1500)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    handleChange(value) {
      console.log(value)
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      // 把上传头像成功之后的图片地址保存起来
      this.goodimg = res.imgUrl
    },
    beforeAvatarUpload(file) {
      const arr = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']
      /* 判断当前的值是否在该数组中 */
      const isBool = arr.includes(file.type)
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isBool) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isBool && isLt2M
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-input {
  width: 180px;
}
.el-textarea {
  width: 300px;
  height: 150px;
}
// /deep/穿刺写法
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
